package com.jckl.foodathon.services.config;

import java.io.IOException;

import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jckl.foodathon.common.constant.CommunicationConstants;
import com.jckl.foodathon.common.utils.BitmapEncode;
import com.jckl.foodathon.model.Store;
import com.jckl.foodathon.sqlite.dao.DaoSession;
import com.jckl.foodathon.sqlite.model.TB_Store;
import com.jckl.foodathon.sqlite.services.SqliteCommon;
import com.jckl.foodathon.sqlite.services.StoreRepository;

public class GetAllStoreInfo {
	
	public void perform(Activity activity){

		PerformGetAllStoreAsync async = new PerformGetAllStoreAsync(activity);
		async.execute();
	}
	
	private class PerformGetAllStoreAsync extends AsyncTask<Void, Void, Boolean> {
		
		Context context;
		
		public PerformGetAllStoreAsync(Activity activity){
			this.context = activity.getApplicationContext();
		}
		
		
		@Override
		protected void onPreExecute() {
			
		}
		
		@Override
		protected Boolean doInBackground(Void... nothing) {
			
			boolean operationComplete = false;
			
			try {
				Gson gson = new Gson();

				ClientResource cr = new ClientResource(CommunicationConstants.SERVER_URI + CommunicationConstants.DATA_STORE);
				Representation rep = cr.get();
				String responseStatus = rep.getText();
				
				//Receive data from server
				Store[] stores = gson.fromJson(responseStatus, Store[].class);
				
				//Insert the data into sqlite
				SqliteCommon sqliteCommon = new SqliteCommon();
				DaoSession session = sqliteCommon.setupDatabase(context);
				//Clear all data in sqlite
				StoreRepository.clearStores(session);
				for (Store store : stores) {
					if(store.isActive()){
						TB_Store tb_store = new TB_Store();
						tb_store.setName(store.getName());
						tb_store.setAddress_unitNumber(store.getAddress_unitNumber());
						tb_store.setAddress_buildingName(store.getAddress_buildingName());
						tb_store.setAddress_streetName(store.getAddress_streetName());
						tb_store.setAddress_area(store.getAddress_area());
						tb_store.setAddress_postcode(store.getAddress_postcode());
						tb_store.setAddress_state(store.getAddress_state());
						tb_store.setAddress_country(store.getAddress_country());
						tb_store.setLongitude(store.getLongitude());
						tb_store.setLatitude(store.getLatitude());
						tb_store.setContactNo(store.getContactNo());
						tb_store.setContactPerson(store.getContactPerson());
						tb_store.setNotes(store.getNotes());
						tb_store.setImage(BitmapEncode.bitmapToByte(store.getImage()));
						tb_store.setWebsite(store.getWebsite());
						tb_store.setEmail(store.getEmail());
						StoreRepository.insertOrUpdate(session, tb_store);
					}
				}
				
				operationComplete = true;	
				
			} catch (IOException IOex) {
				Log.e(CommunicationConstants.APP_TAG, "Unable to parse return value");
				IOex.printStackTrace();
			} catch (Exception ex){
				ex.printStackTrace();
			}
			
			return operationComplete;
		}
		
		@Override
		protected void onProgressUpdate(Void... nothing){
	    }

		
		@Override
		protected void onPostExecute(Boolean responseSuccess) {
			if(responseSuccess){
				Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(context, "Error: ", Toast.LENGTH_SHORT).show();
			}
	    }
	}

}
